<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\Atlas;
use App\Models\AtlasCategory;
use Illuminate\Http\Request;

class AtlasController extends Controller
{


    //相册首页
    public function photo(Request  $request,$category_id = "")
    {
        //获取类别
        $categorys = AtlasCategory::all('id','name');
        $img_path = env('QINIU_IMAGE');
        $param = $request->all();
        $searchTitle = isset($param['searchTitle'])?$param['searchTitle']:"";
        if($searchTitle){
//            $atlas = Atlas::where('title','like',"%".$searchTitle."%")->take(16)->orderBy('id','desc')->get();
            $atlas = Atlas::where('title','like',$searchTitle."%")->orderBy('id','desc')->paginate(8);
        }else{
            if($category_id){
//                $atlas = Atlas::where('category_id',$category_id)->take(16)->orderBy('id','desc')->get();
                $atlas = Atlas::where('category_id',$category_id)->orderBy('id','desc')->paginate(8);
            }else{
//                $atlas = Atlas::orderBy('id','desc')->take(16)->get();
                $atlas = Atlas::orderBy('id','desc')->paginate(8);
            }
        }

        foreach ($atlas as &$atla){
            $image =  $img_path.'/'.$atla->image;
            $atla->image = $image;
        }

        return view('photos.index',[
            'categorys'=>$categorys,
            'atlas'=>$atlas,
        ]);
    }

    //相册详情
    public function details(Request $request,$id = '')
    {
        $img_path = env('QINIU_IMAGE');
        //获取类别
        $categorys = AtlasCategory::all('id','name');
        if($id){
            $atla = Atlas::where('id',$id)->first();
            if(!$atla){
                $localhost = env("APP_URL");
                $key = rand(1,2);
                $img = $localhost."/404/404_".$key.".webp";
                return view("errors.404", [
                    'img' => $img,
                ]);
            }
            $atla->pv += 1;
            $atla->save();
            $images = [];
            $imgs = $atla->atlas;
            foreach ($imgs as &$img){
                $image =  $img_path.'/'.$img;
                $images[] = $image;
            }
            $atla->atlas = $images;

        }else{
            return redirect('photo');
        }

        //推荐
        $randoms = Atlas::where('id','!=',$id)->inRandomOrder()->take(4)->get();//相关推荐
        foreach ($randoms as &$random){
            $image =  $img_path.'/'.$random->image;
            $random->image = $image;
        }

        //上一篇
        $previous = Atlas::where('id','<',$id)->orderBy('id','desc')->first();
        //下一篇
        $next = Atlas::where('id','>',$id)->orderBy('id','desc')->first();

        return view('photos.details',[
            'categorys'=>$categorys,
            'atla'=>$atla,
            'previous'=>$previous,
            'next'=>$next,
            'randoms'=>$randoms,
        ]);

    }

    //搜索
    public function search(Request $request)
    {
        $param = $request->all();
        $searchTitle = isset($param['searchTitle'])?$param['searchTitle']:"";
        $img_path = env('IMAGE_PATH');
        //获取类别
        $categorys = AtlasCategory::all('id','name');
        if($searchTitle){
            $atlas = Atlas::where('title','like',"%".$searchTitle."%")->orderBy('id','desc')->get();
        }else{
            return redirect('photo');
        }
        foreach ($atlas as &$atla){
            $image =  $img_path.'/'.$atla->image;
            $atla->image = $image;
        }

        return view('photos.index',[
            'categorys'=>$categorys,
            'atlas'=>$atlas,
        ]);

    }


}
